package com.github.zxhtom.excel.rowHandler;

import com.alibaba.excel.write.handler.RowWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import org.apache.commons.lang3.time.DurationFormatUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @package com.github.zxhtom.excel.rowHandler
 * @Class AgeRowHandler
 * @Description TODO
 * @Author zhangxinhua
 * @Date 19-11-7 上午9:15
 */
public class AgeRowHandler implements RowWriteHandler {
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Override
    public void beforeRowCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Integer integer, Integer integer1, Boolean aBoolean) {

    }

    @Override
    public void afterRowCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Integer integer, Boolean aBoolean) {

    }

    @Override
    public void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Integer integer, Boolean isHead) {
        if (!isHead) {
            Cell ageCell = row.getCell(2);
            Cell birthCell = row.getCell(4);
            Date birthDate = null;
            try {
                birthDate = simpleDateFormat.parse(birthCell.getStringCellValue());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            Date nowDate = new Date();
            String year = DurationFormatUtils.formatPeriod(birthDate.getTime(), nowDate.getTime(), "y");
            ageCell.setCellValue(year);
        }
    }

    public static void main(String[] args) {
        System.out.println(DurationFormatUtils.formatPeriod(new Date("2012/05/05").getTime(), new Date("2013/08/07").getTime(), "y-M-d"));

    }
}
